<?php
/**
 * 定义 Controller_BoDescription 类
 *
 * @copyright Copyright (c) 2004 - 2009 iTeam
 * @author iTeam Monkey
 * @package XingYaoDianZi
 * @version 200912241029
 */

FLEA::loadClass('Controller_BoBase');
class Controller_BoDescription extends Controller_BoBase
{
    var $_modelClasses;
    var $_modelArticles;
    var $access;
    var $catid;
    var $productid;
    var $_helperFunction;
    var $dbo;

    /**
     * 构造函数
     *
     * @return Controller_BoNews
     */
    function Controller_BoDescription() {
        parent::Controller_BoBase();
        $this->access = PRODUCT_ADMIN;
        $this->catid = PRODUCT_CATDES_CLASS;
        $this->productid = PRODUCT_CLASS;
        //用户角色权限控制
        if(!$this->checkDispatcher($this->access)){
            redirect(url('BoDashboard','ErrorIn'));
        }
        $this->_modelClasses =& FLEA::getSingleton('Model_Categories');
        $this->_modelArticles =& FLEA::getSingleton('Model_Articles');
        $this->_helperFunction =& FLEA::getSingleton('Helper_Function');
        $this->dbo = &FLEA::getDBO();
    }

    /**
     * 显示文章列表
     */
    function actionIndex() {
    	$catid = $this->catid;
        $field = array('class_id' => $catid);        
        $table =& $this->_modelArticles->getTable();
        $page = isset($_GET['page']) ? (int)$_GET['page'] : 0;
        FLEA::loadClass('FLEA_Helper_Pager');
        $pager =& new FLEA_Helper_Pager($table, $page, 10, $field, 'dateline DESC');
        $pk = $table->primaryKey;
        $rowset = $pager->findAll("*",$field);
        $this->_setBack();
//        dump($this->dbo->log);
//		var_dump($rowset);
        $listTitle = '分类介绍';
        include(TPL_DIR . '/BoCatDesList.php');
    }
   // 添加
    function actionCreate() {
        $table =& $this->_modelArticles->getTable();
        $article = $this->_prepareData($table->meta);
        $this->_editArticle($article);
    }
    //保存
    function actionSave() {
        __TRY();
        $user = $this->getUser();
		if(!$_POST['article_id'])$_POST['dateline'] = time();
		$_POST['updated'] = time();
		$_POST['author'] = $user['ID'];
		$_POST['content'] = $_POST['spaw1'];
        $errorMessage = $this->_uploadPicture('postfile');
        if($errorMessage && is_array($errorMessage))$_POST['image_src'] = $errorMessage['filename'];//上传图片
        else unset($_POST['image_src']);
        $arid = $this->_modelArticles->saveArticle($_POST);
        $ex = __CATCH();
        if (__IS_EXCEPTION($ex)) {
            return $this->_editArticle($_POST, $ex->getMessage());
        }
		redirect($this->_url('Index'));
    }

    /**
     * 修改文章
     */
    function actionEdit() {
        $article = $this->_modelArticles->getArticle($_GET['id']);
        $this->_editArticle($article);
    }

    /**
     * 显示文章信息编辑页面
     *
     * @param array $article
     * @param string $errorMessage
     */
    function _editArticle($article, $errorMessage = '') {
    	$class_id = $article['class_id']?$article['class_id']:$_REQUEST['class_id'];
    	$table =& $this->_modelArticles->getTable();
        $pk = $table->primaryKey;
        $pclass=$this->_modelClasses->getClass($this->productid);
        $pclasses=$this->_modelClasses->getSubClasses($pclass);
        include(TPL_DIR . '/BoDesEdit.php');
    }
    //删除资讯
    function actionRemove() {
        $this->_modelArticles->removeArticle($_GET['id']);
        $this->_goBack();
    }

}
